package de.tum.in.eist.poll.client.views.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

import de.tum.in.eist.poll.client.controllers.Controller;
import de.tum.in.eist.poll.client.views.Views;
import de.tum.in.eist.poll.pub.shared.User;

/**
 * 
 * @author Sebastian Hofstetter
 * 
 */
public class LoginWidget extends HorizontalPanel {
	private TextBox usernameTextBox = new TextBox();
	private PasswordTextBox passwordTextBox = new PasswordTextBox();
	private Button loginButton = new Button();
	private Label loginLabel = new Label();
	private DialogBox dialogBox = new DialogBox(true);
	private Label logout = new Label("Logout");

	// is called after the login-operation
	private AsyncCallback<User> login = new AsyncCallback<User>() {

		@Override
		public void onFailure(Throwable caught) {
			dialogBox.center();
		}

		@Override
		public void onSuccess(User result) {
			Controller.controller.loggedInUser = result;
			Controller.controller.showPage(Views.poll);
		}
	};

	// Executes the login-operation on Enter-pressed
	private KeyPressHandler enterHandler = new KeyPressHandler() {

		@Override
		public void onKeyPress(KeyPressEvent event) {
			if (event.getCharCode() == KeyCodes.KEY_ENTER)
				Controller.loginController.login(usernameTextBox.getText(),
						passwordTextBox.getText(), login);
		}

	};

	// Selects the text of the Source TextBox
	private FocusHandler focusHandler = new FocusHandler() {

		@Override
		public void onFocus(FocusEvent event) {
			if (event.getSource() instanceof TextBox) {
				TextBox source = ((TextBox) event.getSource());
				source.setSelectionRange(0, source.getText().length());
			}
		}
	};

	public LoginWidget() {
		loginButton.addStyleName("loginButton");
		
		if (Controller.controller.loggedInUser == null) {
			dialogBox.setText("Login error: Wrong Username or Password.");
			dialogBox.setAnimationEnabled(true);
			dialogBox.setGlassEnabled(true);
			dialogBox.add(new Image("images/thumbsdown.jpg"));
			
			add(usernameTextBox);
			usernameTextBox.setText("Student_1");
			usernameTextBox.addFocusHandler(focusHandler);
			usernameTextBox.addKeyPressHandler(enterHandler);
			usernameTextBox.addStyleName("loginText");

			add(passwordTextBox);
			passwordTextBox.setText("new");
			passwordTextBox.setFocus(true);
			passwordTextBox.addFocusHandler(focusHandler);
			passwordTextBox.addKeyPressHandler(enterHandler);
			passwordTextBox.addStyleName("loginText");
			
			add(loginButton);
			loginButton.setText("Login");
			loginButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					Controller.loginController.login(usernameTextBox.getText(),
							passwordTextBox.getText(), login);
				}
			});
		} else {
			logout.getElement().setId("logoutMenu");
			logout.setVisible(false);
			logout.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					Controller.loginController.logout();
				}
			});
			add(logout);
			
			loginLabel.setText(Controller.controller.loggedInUser.getName());
			loginLabel.addStyleName("loginLabel");
			loginLabel.addMouseOverHandler(new MouseOverHandler() {
				@Override
				public void onMouseOver(MouseOverEvent event) {
					logout.setVisible(!logout.isVisible());
				}
			});
			loginLabel.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					logout.setVisible(!logout.isVisible());
				}
			});
			add(loginLabel);
		}
		
		setStylePrimaryName("login");
	}
}
